"""
CompMakerPlayer.py

Author: Zach Epolito, Miles Malerba
Date: 10-11-08

$Id: CompMakerPlayer.py 339 2008-10-17 03:57:13Z malerba89 $
"""

from WaitFrame import WaitFrame
from random import randint
from Game import Game
from MakerMove import MakerMove
from MoveCommand import MoveCommand
from BreakerMove import BreakerMove
from copy import copy
import time

"""represents a random guessing Maker"""
class CompMakerPlayer:

    #Frame for getting Maker input
    frame = None

    """Constructor."""
    def __init__(self, game, host, gui, waitTime):
        self.game = game
        self.host = host
        self.gui = gui
        self.waitTime = waitTime
        
        game.register_observer(self)
        
        self.code = BreakerMove([randint(0,5), randint(0,5), randint(0,5), randint(0,5)])

    def update(self, subject, param):

        if subject == self.game: 
            if ((param == self.game.NOTIFY_MOVE) or (param == self.game.NOTIFY_UNDO)):
                if self.game.turn == self.game.MAKER_TURN : 
                    if self.frame == None:
                            self.frame = WaitFrame(self.gui.getRootInputView(), width=300, height=100)

                    self.gui.set_input_view(self.frame)
                    self.frame.update()
                    self.frame.update()
                    # sleep
                    time.sleep(self.waitTime)

                    #Creates a correct maker move
                    feedback = self.generate_feedback(self.game.get_last_move())
                    
                    self.moveCommand.execute(feedback)

    """takes a move and based on the code
       determines what feedback pegs to provide
    """
    def generate_feedback(self, move):
        feedback = []
        move_pieces = self.copyMove(move).get_pieces()
        code_copy = self.copyMove(self.code)
        code_copy = code_copy.get_pieces()
        i=0

        while i != len(move_pieces):
            if move_pieces[i] == code_copy[i]:
                feedback.append(MakerMove.BLACK)
                del move_pieces[i]
                del code_copy[i]
            else:
                i += 1
        for piece in move_pieces:
            if piece in code_copy:
                feedback.append(MakerMove.WHITE)
                code_copy.remove(piece)
        for piece in code_copy:
            feedback.append(MakerMove.NONE)
        return MakerMove(feedback)
        
    def setStartingCode(self, startFunctor):
        startFunctor()

    def setWaitTime(self, waitTime):
        self.waitTime = waitTime

    """handle if new game or exit command is executed"""
    def handleCommandExecuted(self, cmd):
        pass

    def setCommands(self, undoCommand, winCommand, moveCommand, exitCommand, newGameCommand):
        self.moveCommand = moveCommand

    def copyMove(self, originalMove):
        moveList = originalMove.get_pieces()
        newList = [None, None, None, None]
        peg = 0
        while (peg < 4):
            newList[peg] = moveList[peg]
            peg = peg + 1
        
        return MakerMove(newList)
        
